package cn.com.oceansoft.business.szjf.service.impl;

import cn.com.oceansoft.business.base.entity.Pager;
import cn.com.oceansoft.business.base.entity.Query;
import cn.com.oceansoft.business.sys.entity.SysUser;
import cn.com.oceansoft.business.sys.persistence.ISysUserMapper;
import cn.com.oceansoft.business.szjf.entity.Personpostmap;
import cn.com.oceansoft.business.szjf.entity.Position;
import cn.com.oceansoft.business.szjf.persistence.IPszlMapper;
import cn.com.oceansoft.business.szjf.persistence.IZczhMapper;
import cn.com.oceansoft.business.szjf.service.IZhshService;
import cn.com.oceansoft.nos.common.util.web.PageUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Administrator on 2017/6/26.
 */
@Service
@Transactional
public class ZhshService implements IZhshService {
    @Resource
    private ISysUserMapper sysUserMapper;
    @Resource
    private IPszlMapper pszlMapper;
    @Resource
    private IZczhMapper zczhMapper;

    /**
     * 获取注册账号
     * @param i 0:没有审核 1：已经审核
     * @return
     */
    @Override
    public List<SysUser> getZczh(String i) {
        return sysUserMapper.getZczh(i);
    }

    @Override
    public Pager<SysUser> findZczhByPage(Query query) {
        PageHelper.startPage(query.getPageNum(), query.getNumPerPage(), true);
        Page<SysUser> page = (Page<SysUser>) sysUserMapper.findZczhByCondition((SysUser)query.getObj());
        return PageUtil.page2pager(page, query);
    }

    @Override
    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public void approvalByGuid(SysUser user) {
        sysUserMapper.approvalByGuid(user);
        //分配用户权限
        Personpostmap personpostmap = new Personpostmap();
        personpostmap.setUserguid(user.getGuid());
        List<Position> positions = pszlMapper.getDistributePosition("2");//普通用户权限
        if(positions != null && positions.size()>0){
            personpostmap.setPositionguid(positions.get(0).getGuid());
        }
        zczhMapper.insertPersonPostMap(personpostmap);
    }
}
